<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      size="small"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="模板名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="模板名称"></el-input>
      </el-form-item>

      <el-form-item label="用户状态" prop="dataState">
        <el-radio-group v-model="dataForm.dataState">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>

          <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { roleList } from "@/api/role";
import { saveAndUpdate } from "@/api/userimport";
export default {
  data() {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        name: "",
        fields: [],
        dataState: 0,
        remark:"",
      },
      dataRule: {
        name: [
          { required: true, message: "模板名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(data) {
      this.dataForm.id = 0;
      if (data) {
        this.dataForm.id = data.id || 0;
      }
      roleList({})
        .then((res) => {
          if (res && res.code === 0) {
            this.roleList = res.data;
          } else {
            this.roleList = [];
          }
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.dataForm.id = data.id;
            this.dataForm.name = data.name;
            this.dataForm.remark = data.remark;
            this.dataForm.fields = data.fields || [];
            this.dataForm.dataState = data.dataState;
          }
        });
    },
    handleCheckedRoleIdsChange(value) {
      let checkedCount = value.length;
      debugger;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          saveAndUpdate({
            id: this.dataForm.id || "",
            name: this.dataForm.name,
            remark: this.dataForm.remark,
            dataState: this.dataForm.dataState,
            fields: this.dataForm.fields || [],
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
